package org.misty.practices.algorithm.hanoi.recur;

import java.util.ArrayList;
import java.util.List;

public class HanoiRecursion {
    private List<String> steps;
    private Tower a;
    private Tower b;
    private Tower c;

    public HanoiRecursion(int level) {
        reset(level);
    }

    public void reset(int level) {
        this.steps = new ArrayList<>();
        this.a = new Tower("a", level);
        this.b = new Tower("b", 0);
        this.c = new Tower("c", 0);
    }

    public void start() {
        move(a, b, c, a.getLevel());
    }

    public void printSteps() {
        if (steps != null && !steps.isEmpty()) {
            for (var step : steps) {
                System.out.println(step);
            }
        }
    }

    private void move(Tower src, Tower by, Tower dest, int level) {
        if (level > 1) {
            move(src, dest, by, level - 1);
            src.moveHeadTo(dest);
            steps.add(src.getName() + " -> " + dest.getName());
            move(by, src, dest, level - 1);
        } else if (level == 1) {
            src.moveHeadTo(dest);
            steps.add(src.getName() + " -> " + dest.getName());
        }
    }

    public static void main(String[] args) {
        var hanoi = new HanoiRecursion(5);
        hanoi.start();
        hanoi.printSteps();
    }
}
